package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbTaskReportingBo;
import com.ruoyi.tb.lmt.domain.vo.TbTaskReportingVo;
import com.ruoyi.tb.lmt.domain.TbTaskReporting;
import com.ruoyi.tb.lmt.mapper.TbTaskReportingMapper;
import com.ruoyi.tb.lmt.service.ITbTaskReportingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 任务报工Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class TbTaskReportingServiceImpl implements ITbTaskReportingService {

    private final TbTaskReportingMapper baseMapper;

    @Autowired
    private TbTaskReportingMapper tbTaskReportingMapper;

    /**
     * 查询任务报工
     */
    @Override
    public TbTaskReportingVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询任务报工列表
     */
    @Override
    public TableDataInfo<TbTaskReportingVo> queryPageList(TbTaskReportingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbTaskReporting> lqw = buildQueryWrapper(bo);
        Page<TbTaskReportingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务报工列表
     */
    @Override
    public List<TbTaskReportingVo> queryList(TbTaskReportingBo bo) {
        LambdaQueryWrapper<TbTaskReporting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbTaskReporting> buildQueryWrapper(TbTaskReportingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbTaskReporting> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWorkType()), TbTaskReporting::getWorkType, bo.getWorkType());
        lqw.eq(StringUtils.isNotBlank(bo.getFormConfig()), TbTaskReporting::getFormConfig, bo.getFormConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getFillData()), TbTaskReporting::getFillData, bo.getFillData());
        lqw.eq(StringUtils.isNotBlank(bo.getPrintConfig()), TbTaskReporting::getPrintConfig, bo.getPrintConfig());
        lqw.eq(bo.getCreateTime() != null, TbTaskReporting::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbTaskReporting::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbTaskReporting::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbTaskReporting::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    /**
     * 新增任务报工
     */
    @Override
    public Boolean insertByBo(TbTaskReportingBo bo) {
        TbTaskReporting add = BeanUtil.toBean(bo, TbTaskReporting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改任务报工
     */
    @Override
    public Boolean updateByBo(TbTaskReportingBo bo) {
        TbTaskReporting update = BeanUtil.toBean(bo, TbTaskReporting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbTaskReporting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除任务报工
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TbTaskReportingVo> selectList() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long workGroupId = loginUser.getDeptId();
        return tbTaskReportingMapper.selectTaskList(workGroupId);
    }
}
